import { getInputinventory,
    getRawinventory,
    getfactorycount,
    getOrdersCountByCity, 
    getfarmingBasecount, 
    getBaseId,
    farmingproduct,
    supplyin,
    meterial,
    getfactoryinfo,
 } from '../../../Api/leadership/Consumption'
import { getOrdersCount } from '../../../Api/leadership/logistics'
import Store from '../../../store/index'

export function getInputinventorya() {
    const datainfo = []
    const obj = {}
        getInputinventory(1).then(res => {
            if(res.data.data) {
                res.data.data.records.forEach(item => {
                    datainfo.push({
                    name: item.投入品名称,
                    value: item.剩余数量
                  })
                })
            }
        })
    obj.data = datainfo
    Store.commit('setInputinventory',obj)
}

export function getRawinventorya() {
    getRawinventory().then(res => {
        const datainfo = []
        const obj = {}
        if(res.data.data) {
            res.data.data.records.forEach(item => {
                datainfo.push({
                name: item.名称,
                value: item.库存数量
              })
            })
            obj.data = datainfo
            Store.commit('setRawinventory',obj)
        }
    })
}

export function getfactorycounta() {
    getfactorycount().then(res => {
        Store.commit('setfactorycount',res.data.data)
    })
}

export function getOrdersCounta() {
    getOrdersCount().then(res => {
        Store.commit('setOrdersCount',res.data.data)
    })
}

export function getOrdersCountByCitya() {
    getOrdersCountByCity().then(res => {
        let totalcount = 0
        res.data.data.forEach(item => {
            totalcount = totalcount + item.count
        })
        Store.commit('setallCount',totalcount)
    })
}

export function getfarmingBasecounta() {
    getfarmingBasecount().then(res => {
        Store.commit('setfarmingBasecount',res.data.data)
    })
}

// 养殖基地信息
export function getframing() {
    getBaseId().then(res => {
        Store.commit('setfarmingbase',res.data.data)
    })

}

export function farmingproducta() {
    farmingproduct().then(res => {
        let sum = 0
        res.data.data.forEach(item => {
            sum = sum + item.产品库存量
        })
        Store.commit('setfarmingproduct',sum)
    })
}

export function supplyina() {
    supplyin().then(res => {
        let data = []
        if(res.data.data) {
            res.data.data.records.forEach(item => {
                data.push([
                    item.入库类型,
                    item.投入品名称,
                    item.购买数量,
                    item.供应商名称
                ])
            })
            Store.commit('setsupplyin',data)
            localStorage.setItem('leaderdata',JSON.stringify(Store.state.Consumpt))
        }
    })
}

export function setdata() {
    getall().then(() => {
        localStorage.setItem('leaderdata',JSON.stringify(Store.state.Consumpt))
    })
}

export function getleaderdata() {
    Store.state.Consumpt = JSON.parse(localStorage.getItem('leaderdata'))
}

// 原辅料入库信息
export function meteriala() {
    meterial().then(res => {
        let data = []
        if(res.data.data) {
            res.data.data.forEach(item => {
                for(let i=0;i<12;i++) {
                    data.push([
                        item.创建时间,
                        item.名称,
                        item.单价,
                        item.数量,
                    ])
                }
            })
            Store.commit('setmeterial',data)
            localStorage.setItem('leaderdata',JSON.stringify(Store.state.Consumpt))
        }
    })
}

// 加工厂信息
export function getfactoryinfoa() {
    getfactoryinfo().then(res => {
        Store.commit('setfactorys',res.data.data)
        console.log(Store.state);
    })
}

export function getall() {
    return new Promise((resolve,reject) => {
        getframing()
        getfactorycounta()
        getRawinventorya()
        getInputinventorya()
        getOrdersCounta()
        getOrdersCountByCitya()
        getfarmingBasecounta()
        farmingproducta()
        supplyina()
        meteriala()
        getfactoryinfoa()
        resolve()
    })
}